package org.fnzn.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.fnzn.bean.*;
import org.fnzn.bean.vo.User;
import org.fnzn.core.Constants;
import org.fnzn.core.Page;
import org.fnzn.core.Result;
import org.fnzn.dao.FnglProjectMapper;
import org.fnzn.service.FnglProjectService;
import org.fnzn.service.FnglStaffService;
import org.fnzn.service.base.AbstractService;
import org.fnzn.util.ResultGenerator;
import org.fnzn.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.*;

@Service
public class FnglProjectServiceImpl  extends AbstractService<FnglProject> implements FnglProjectService {

    @Autowired(required = false)
    private FnglProjectMapper fnglProjectMapper;
    @Autowired
    private FnglStaffService fnglStaffService;
    @Autowired
    private UserRoleServiceImpl userRoleServiceImpl;
    @Override
    public Result findFnglProject(FnglProject fnglProject, Page page) {
        // TODO Auto-generated method stub
        if(page.getLimit()!=null&& page.getPage()!=0){
            Condition condition = new Condition(FnglProject.class);
            Criteria criteria=condition.createCriteria();
//            if(StringUtils.isNotBlank(fnglProject.getc)) {
//                criteria.andCondition("OPER_DATE >=", (fnglProject.getCreateDate()+"").replace("/", ""));
//            }
//            if(StringUtils.isNotBlank(fnglProject.getMtnDate())) {
//                criteria.andCondition("OPER_DATE <=", (fnglProject.getMtnDate()+"").replace("/", ""));
//            }
//            if(StringUtils.isNotBlank(fnglProject.getOrgCd())){
//                criteria.andEqualTo("orgCd",sysLog.getOrgCd());
//            }
//            if(StringUtils.isNotBlank(fnglProject.getProjectId())){
//                String[] userIDs = sysLog.getUserId().toString().split(",");
//                List<String> userIds= Arrays.asList(userIDs);
//                criteria.andIn("userId", userIds);
//            }
            // 根据删除状态查询
            criteria.andEqualTo("delFlag",0);
            // 根据搜索框条件查询
            if(!StringUtil.isEmpty(fnglProject.getName())){
                criteria.andLike("name","%"+fnglProject.getName()+"%");
            }
            condition.setOrderByClause("create_time desc");
            PageHelper.startPage(page.getPage(),page.getLimit(), true);
            List<FnglProject> list = findByCondition(condition);
            page.setRows(list);
            page.setTotal(new PageInfo(list).getTotal());
            return ResultGenerator.genSuccessResult(page);
        }
        return ResultGenerator.genFailResult("查询失败");
    }

    @Override
    public Result insertFnglProject(FnglProject fnglProject) {
        if(fnglProject!=null){
            fnglProject.setProjectId(StringUtil.getUUID());
        }else{
            return ResultGenerator.genFailResult("项目为空");
        }
        fnglProject.setCreateTime(new Date());
        int n = save(fnglProject);
        if(n>0){
            return ResultGenerator.genSuccessResult(fnglProject);
        }else{
            return ResultGenerator.genFailResult("添加失败");
        }
    }

    @Override
    public Result updateFnglProject(FnglProject fnglProject) {
        fnglProject.setUpdateTime(new Date());
        int n = update(fnglProject);
        if(n>0){
            return ResultGenerator.genSuccessResult(fnglProject);
        }else{
            return ResultGenerator.genFailResult("更新失败");
        }
    }

    @Override
    public Result deleteFnglProject(String projectId) {

        boolean flag = fnglProjectMapper.deleteFnglProject(projectId);
        if(flag){
            return ResultGenerator.genSuccessResult("删除成功");
        }else{
            return ResultGenerator.genFailResult("删除失败");
        }
    }

    @Override
    public Result deleteAllFnglProject(String projectIds) {

        if(!StringUtil.isEmpty(projectIds)){

            String[] projectArr = projectIds.split(",");
            int n = 0;
            for(String item : projectArr){
                boolean flag = fnglProjectMapper.deleteFnglProject(item);
                if(flag){
                    n+=1;
                }
            }
            if(n == projectArr.length){
                return ResultGenerator.genSuccessResult("删除成功");
            }else{
                return ResultGenerator.genFailResult("删除失败");
            }

        }else{
            return ResultGenerator.genFailResult("删除失败");
        }
    }

    @Override
    public List<FnglProject> getProjectList(String projectType) {

        Condition condition = new Condition(FnglProject.class);
        Criteria criteria=condition.createCriteria();
        criteria.andEqualTo("delFlag",0);

        if(StringUtil.isEmpty(projectType)){
            List<FnglProject> list = findByCondition(condition);
            return list;
        }
        if(projectType.equals(Constants.NEWHOUSE)){
            criteria.andEqualTo("projectType",0);
        }else{
            criteria.andEqualTo("projectType",1);
        }
        List<FnglProject> list = findByCondition(condition);
        return list;
    }

    @Override
    public List<FnglProjectType> getProjectType() {

       FnglProjectType fnglProjectType1 = new FnglProjectType();
       fnglProjectType1.setRealName("新房项目");
       fnglProjectType1.setProjectType("0");
       FnglProjectType fnglProjectType2= new FnglProjectType();
       fnglProjectType2.setRealName("二手房项目");
       fnglProjectType2.setProjectType("1");
       List<FnglProjectType> list = new ArrayList<FnglProjectType>();
       list.add(fnglProjectType1);
       list.add(fnglProjectType2);

       return list;
    }

    @Override
    public String[] getProjectImg(String projectId) {

        // 根据项目id获取到该项目详情
        FnglProject fnglProject = findById(projectId);
        if(fnglProject == null){
            return null;
        }else{
            // 定义新数组
            String[] newImgs = new String[1];
            // 获取到图片
            String imgs = fnglProject.getImg();
            if(!StringUtil.isEmpty(imgs)){
                if(imgs.contains(",")){
                    newImgs = imgs.split(",");
                }else{
                    newImgs[0] = imgs;
                }
                return newImgs;
            }else{
                return null;
            }
        }
    }

    /**
     * 获取到所属项目id和所属项目名称
     * @return
     */
    @Override
    public Result getFnglProject(User user){

        if(user == null){
            return ResultGenerator.genFailResult("查询失败");
        }
        // 获取到当前登录用户所属角色
        LrdUserrole userrole = userRoleServiceImpl.findBy("userId",user.getUser_id());
        String roleId = userrole.getRoleId();
        Condition condition = new Condition(FnglProject.class);
        Example.Criteria criteria=condition.createCriteria();
        criteria.andEqualTo("delFlag",0);
        // 如果超级管理员登录，那么可以看到所有项目
        if(roleId.equals(Constants.ADMIN_ROLE)){
            List<FnglProject> fnglProjects = findByCondition(condition);
            return ResultGenerator.genSuccessResult(fnglProjects);
        }
        // 获取到当前登录用户所属项目
        String projectId = user.getProjectId();
        // 根据项目id获取到对应项目
        FnglProject fnglProject = findById(projectId);
//        // 如果当前登录用户所属项目的项目类型为新房，那么只能看到新房项目
//        if(fnglProject.getProjectType().equals(Constants.NEWHOUSE)){
//            criteria.andEqualTo("projectType",Constants.NEWHOUSE);
//        }else{
//            criteria.andEqualTo("projectType",Constants.SECONDHANDHOUSE);
//        }
        List<FnglProject> fnglProjects = findByCondition(condition);
        return ResultGenerator.genSuccessResult(fnglProjects);

    }

    /**
     *  录入房源时获取到所有的二手房项目
     */
    @Override
    public Result getHouseProject() {

        Condition condition = new Condition(FnglProject.class);
        Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("delFlag",0);
        criteria.andEqualTo("projectType",Constants.SECONDHANDHOUSE);
        List<FnglProject> fnglProjects = findByCondition(condition);
        return ResultGenerator.genSuccessResult(fnglProjects);
    }
}
